# 生男生女几率相同，一个家庭会持续生孩直到生出男孩，求男女比例

import random
import matplotlib.pyplot as plt
import matplotlib
font = {'family': 'SimHei'}
matplotlib.rc('font', **font)

# 每次生到男孩为止的函数
def f():
    count = 0
    girl = 0
    while 1:
        count += 1
        if random.randint(0, 1):
            girl += 1
        else:
            break
    ls_girl.append(girl)    # 女生人数
    ls_count.append(count)  # 总人数
    return girl, count, girl/count

ls_girl, ls_count = [], []
rate_ls = []
k = 10000          # k户家庭
for i in range(k):
    f()
    rate_ls.append(sum(ls_girl)/sum(ls_count))
plt.plot(rate_ls)
plt.show()
